package org.example.practiceexamples.mapper;

import org.example.practiceexamples.dox.Lab;
import org.example.practiceexamples.dox.User;
import org.example.practiceexamples.dto.LabAppointment;
import org.example.practiceexamples.dto.UserLab;
import org.springframework.jdbc.core.ResultSetExtractor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class UserLabResultSetExtractor implements ResultSetExtractor<UserLab> {
    @Override
    public UserLab extractData(ResultSet rs) throws SQLException{
        User user = null;
        List<Lab> labs = new ArrayList<>();
        while (rs.next()) {
            if(user == null) {
                user = User.builder()
                        .id(rs.getString("u.id"))
                        .name(rs.getString("u.name"))
                        .phone(rs.getString("phone"))
                        .description(rs.getString("u.description"))
                        .role(rs.getString("role"))
                        .status(rs.getInt("status"))
                        .account(rs.getString("account"))
                        .password(rs.getString("password"))
                        .profession(rs.getString("profession"))
                        .updateTime(rs.getObject("update_time", LocalDateTime.class))
                        .createTime(rs.getObject("create_time", LocalDateTime.class))
                        .build();
            }
            Lab lab = Lab.builder()
                    .id(rs.getString("l.id"))
                    .name(rs.getString("l.name"))
                    .state(rs.getInt("state"))
                    .description(rs.getString("l.description"))
                    .manage(rs.getString("manage"))
                    .quantity(rs.getInt("quantity"))
                    .build();
            labs.add(lab);
        }
        return UserLab.builder()
                .labs(labs)
                .user(user)
                .build();
    }
}
